package com.cx.bank.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.cx.bank.actionsforms.OperationFrom;
import com.cx.bank.hibernateModel.PageModel;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;

public class OperationActions extends BaseAction {
	// 实例化业务层对象managerimp、模型层对象moneyBean和模型层对象user，注：managerimp和moneyBean都为单例对象
	MoneyBean moneyBean = MoneyBean.getInstance();
	ManagerImpl managerimp = ManagerImpl.getInstance();
	UserBean user = new UserBean();

	/**
	 * 如果没有传递任何标识参数（如command参数），则默认调用unspecified方法
	 */
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("OperationActions=>>unspecified()");
		return mapping.findForward("login_invalid");
	}

	/**
	 * 查询
	 */
	public ActionForward inquiry(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 从moneyBean得到当前余额
		Double money = managerimp.inquiry(moneyBean);
		// 将当前余额设置到request
		request.setAttribute("money", money);
		// 查询成功，请求跳转
		return mapping.findForward("inquiry");
	}

	/**
	 * 存款
	 */
	public ActionForward deposit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 从HttpServletRequest中得到session对象
		HttpSession session = request.getSession();
		// 从session中取得当前用户的用户名
		String username = (String) session.getAttribute("name");
		// 将用户数据设置到userBean
		user.setName(username);
		OperationFrom of = (OperationFrom) form;
		// 从request得到用户需要存储的金额
		String storemoney = of.getMoney();
		ActionMessages messages = new ActionMessages();
		try {
			// 将从表单中得到的String类型的money强制转换成double类型
			Double money = Double.valueOf(storemoney);
			// 调用业务层的存款方法
			managerimp.deposit(moneyBean, money, user);
			// 创建国际化消息文本
			ActionMessage message = new ActionMessage("deposit.success");
			messages.add("deposit_success", message);
			// 传递国际化消息文本
			this.saveErrors(request, messages);
		} catch (InvalidDepositException e) {
			// 创建国际化消息文本
			ActionMessage message = new ActionMessage("deposit.error.InvalidDepositException");
			messages.add("deposit_error1", message);
			// 传递国际化消息文本
			this.saveErrors(request, messages);
			System.out.println("存款失败，用户输入小于零的金额!");
		} catch (NumberFormatException e2) {
			// 创建国际化消息文本
			ActionMessage message = new ActionMessage("deposit.error.NumberFormatException");
			messages.add("deposit_error2", message);
			// 传递国际化消息文本
			this.saveErrors(request, messages);
			System.out.println("存款失败，用户输入了非法字符!");
		}

		return mapping.findForward("deposit");
	}

	/**
	 * 取款
	 */
	public ActionForward withdraw(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 从HttpServletRequest中得到session对象
		HttpSession session = request.getSession();
		// 从session中取得当前用户的用户名
		String username = (String) session.getAttribute("name");
		// 将用户数据设置到userBean
		user.setName(username);
		OperationFrom of = (OperationFrom) form;
		// 从request得到用户需要取出的金额
		String getmoney = of.getMoney();
		// 初始化信息
		String success_info = null;
		// 调用业务层的取款方法
		managerimp.withdrawals(moneyBean, getmoney, user);
		// 取款成功则将错误信息改为 "取款成功!"，并放入到request中
		success_info = "取款成功!";
		request.setAttribute("success_info", success_info);
		return mapping.findForward("withdraw");
	}

	/**
	 * 转账
	 */
	public ActionForward transfer(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 从HttpServletRequest中得到session对象
		HttpSession session = request.getSession();
		// 从session中取得当前用户的用户名
		String username = (String) session.getAttribute("name");
		// 将用户数据设置到userBean
		user.setName(username);
		// 从OperationFrom获得表单信息
		OperationFrom of = (OperationFrom) form;
		// 从OperationFrom中得到要转入的账号
		String transfername = of.getSendname();
		// 从OperationFrom得到用户需要转账的金额
		String sendmoney = of.getSendmoney();
		// 初始化错误信息
		String error_info = null;
		// 限制转账者不能转账给自己
		if (username.equals(transfername)) {
			error_info = "转账失败，请不要转给自己!";
			request.setAttribute("error_info", error_info);
			return mapping.findForward("transfer");
		}
		// 调用业务层的转账方法
		if (managerimp.transfer(moneyBean, sendmoney, transfername, user)) {
			error_info = "转账成功!";
		} else {
			error_info = "转账失败，对方账户错误或不存在!";
		}
		request.setAttribute("error_info", error_info);

		return mapping.findForward("transfer");
	}

	/**
	 * 查询交易明细
	 */
	public ActionForward inquiry_details(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("name");

		// 获取从页面表单中提交过来的值
		OperationFrom of = (OperationFrom) form;
		session.setAttribute("orderby", of.getOrderby());
		
		System.out.println(of.getPageSize());
		System.out.println(of.getPageNo());
		System.out.println(of.getOrderby());
		// 调用业务逻辑操作
		PageModel pageModel = managerimp.findAllTransaction(of.getPageNo(), of.getPageSize(), username,of.getOrderby());

		// 将查询结果放到request中
		request.setAttribute("pagemodel", pageModel);
		return mapping.findForward("inquiry_details");
	}

}
